import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
// import MyInput from '../components/MyInput'
import Register from '../views/Register.vue'
import Home from '../views/Home.vue'
import proIndex from '../views/profile/Index.vue'
// import Profilebar from '../components/Profilebar.vue'
import Edit from '../views/profile/edit.vue'
// import Linex from '../components/Linex.vue'
import About from '../views/About.vue'
import Test from '../views/Test.vue'
import Follow from '../views/profile/Follow.vue'
import Testo from '../views/Testo.vue'
import Star from '../views/profile/Star.vue'
import PostDetail from '../views/PostDetail.vue'
import Comment from '../views/profile/Comment.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/Login',
    name: 'Login',
    component: Login
  },
  {
    path:'/test',
    component:Test
  },
  // {
  //   path: '/MyIput',
  //   name: 'MyIput',
  //   component: MyInput
  // },
  {
    path: '/Register',
    name: 'Register',
    component: Register
  },
  // {
  //   path: '/btn',
  //   name: 'Btn',
  //   component: Btn
  // },
  {
    path: '/proIndex',
    component: proIndex
  },
  // {
  //   path: '/profilebar',
  //   component: Profilebar
  // },
  {
    path: '/edit',
    component: Edit
  },
  // {
  //   path:'/linex',
  //   component:Linex
  // },
  {
    path: '/about',
    component: About,
    // meta:{
    //   needAuth:true
    // }
  },
  {
    path:'/follow',
    component:Follow
  },
  {
    path:'/testo',
    component:Testo
  },
  {
    path:'/star',
    component:Star
  },
  {
    path:'/postdetail/:id',
    component: PostDetail
  },
  {
    path:'/comment/:id',
    component: Comment
  }
]

const router = new VueRouter({
  routes
})

//这是全局路由守卫
router.beforeEach((to, from, next) => {
  //方法1,如果我进入的是个人中心直接用||进行逻辑判断
  // if(to.path==='/proIndex'||to.path==='/edit'){

  // }
  //方法2 把地址装在一个数组里面,判断条件为>-1这样只会接受在数组里面的两个地址
  //   const pageNeedAuth=[
  //     '/proindex',
  //     '/edit'
  //   ]
  //  if (pageNeedAuth.indexOf(to.path) > -1)
  // console.log(to.path);  //to.path是路径
  //方法3 在注册的后面加上一个meta{needAuth:'true'}
  //if(to.meta.needAuth)如果有就是true,没有的话自然就是undifind


  if (to.path === '/proindex') {
    if (localStorage.getItem('token')) {
      return next()
    } else {
      router.push('/Login')
      return next()
    }
  }
  return next()
  
})

export default router